import {
  AddArticleCommnetResponse,
  ArticleCommentResponse,
  ArticleDetailResponse,
} from '@/types/data'
import { RootThunkAction } from '@/types/store'
import http from '@/utils/http'
import dayjs from 'dayjs'
import localizedFormat from 'dayjs/plugin/localizedFormat'
dayjs.extend(localizedFormat)
export const getArticle = (id: string): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ArticleDetailResponse>(`/articles/${id}`)
    const detail = res.data.data
    dispatch({
      type: 'article/get',
      payload: {
        ...detail,
        // 格式化日期：
        pubdate: dayjs(detail.pubdate).locale('zh-cn').format('LL'),
      },
    })
  }
}
//关注作者
export const followAuthor = (
  authorId: string,
  isFollowed: boolean
): RootThunkAction => {
  return async (dispatch) => {
    if (isFollowed) {
      // 说明：现在已经是关注的状态，所以，此时要取消关注
      await http.delete(`/user/followings/${authorId}`)
    } else {
      // 说明：现在还没有关注，所以，此时要关注作者
      await http.post('/user/followings', { target: authorId })
    }
    dispatch({
      type: 'article/update',
      payload: {
        name: 'is_followed',
        value: !isFollowed,
      },
    })
  }
}
//收藏
export const collectArticle = (
  art_id: string,
  is_collected: boolean
): RootThunkAction => {
  return async (dispatch) => {
    if (is_collected) {
      //取消收藏
      await http.delete(`/article/collections/${art_id}`)
    } else {
      //收藏
      await http.post('/article/collections', { target: art_id })
    }

    dispatch({
      type: 'article/update',
      payload: { name: 'is_collected', value: !is_collected },
    })
  }
}
//点赞
export const likeArticle = (
  art_id: string,
  attitude: number
): RootThunkAction => {
  return async (dispatch) => {
    if (attitude === 1) {
      //取消点赞
      await http.delete(`/article/likings/${art_id}`)
    } else {
      //点赞
      await http.post('/article/likings', { target: art_id })
    }
    dispatch({
      type: 'article/update',
      payload: { name: 'attitude', value: attitude === 1 ? 0 : 1 },
    })
  }
}
// 获取文章评论数据
export const getArticleComments = (
  type: string,
  targetId: string,
  offset: string | null
): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ArticleCommentResponse>('/comments', {
      params: {
        type,
        source: targetId,
        offset,
      },
    })
    console.log(res)
    // console.log(res.data.data)

    dispatch({ type: 'article/getArticleComments', payload: res.data.data })
  }
}
//第一次获取评论数据
export const getArticleCommentsFirst = (
  type: string,
  targetId: string,
  offset: string | null
): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ArticleCommentResponse>('/comments', {
      params: {
        type,
        source: targetId,
        offset,
      },
    })
    console.log(1)
    dispatch({
      type: 'article/getArticleCommentsFirst',
      payload: res.data.data,
    })
  }
}
//发表评论
export const addArticleComment = (
  art_id: string,
  value: string
): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.post<AddArticleCommnetResponse>('/comments', {
      target: art_id,
      content: value,
    })
    dispatch({
      type: 'article/addArticleComment',
      payload: res.data.data.new_obj,
    })
  }
}
//为评论点赞
export const articleCommentThumbUp = (
  art_id: string,
  is_liking: boolean
): RootThunkAction => {
  return async (dispatch) => {
    if (is_liking) {
      //取消点赞
      await http.delete(`/comment/likings/${art_id}`)
    } else {
      //取消点赞
      await http.post('/comment/likings', {
        target: art_id,
      })
    }
    dispatch({
      type: 'artilce/articleCommentThumbUp',
      payload: {
        id: art_id,
        is_liking: !is_liking,
      },
    })
  }
}
